package joodle.ui;

import java.awt.GridLayout;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import joodle.domain.ShapeFactory;
import joodle.si.AddonManager;
/**
 * This class is a representation of the panels for tool bar
 *
 */
@SuppressWarnings("serial")
public class ToolsPanel extends JPanel {

	public ToolsPanel() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		add(creatorTools());
		add(editTools());
		add(transformTools());
		add(miscTools());
		
	}
	/**
	 * Creates the panel for the creator tools
	 * @return a JComponent object representing the creator tools
	 */
	private JComponent creatorTools() {
		ShapeFactory[] sfs = AddonManager.factories();
		Action[] as = new Action[sfs.length];
		for(int i = 0; i < sfs.length; ++i)
			as[i] = new ShapeCreatorAction(sfs[i]);
		
		return createPanel("Shapes", as);
	}
	/**
	 * Creates the panel for the edit tools
	 * @return a JComponent object representing the edit tools
	 */
	private JComponent editTools() {
		return createPanel("Edit Tools", new Action[] {
				new PointSelectionToolAction(),
				new RectangularSelectionToolAction(),
				new PointEditToolAction()
		});
	}
	/**
	 * Creates the panel for the transform tools
	 * @return a JComponent object representing the transform tools
	 */
	private JComponent transformTools() {
		return createPanel("Transformation Tools", new Action[] {
				new TranslationToolAction(),
				new RotationToolAction(),
				new ScaleToolAction()
		});
	}
	/**
	 * Creates the panel for the miscellaneous tools
	 * @return a JComponent object representing the miscellaneous tools
	 */
	private JComponent miscTools() {
		return createPanel("Misc Tools", new Action[] {
				new GroupAction(),
				new UnGroupAction(),
				new CopyAction(),
				new DeleteAction() 
		});
	}

	/**
	 * Creates a panel for tools
	 * @return a JComponent object representing the tools
	 */
	private JComponent createPanel(String title, Action[] actions) {
		JPanel cp = new JPanel();
		
		cp.setBorder(BorderFactory.createTitledBorder(title));
		int nrows = actions.length / 2 + (actions.length % 2);
		
		cp.setLayout(new GridLayout(nrows, 2));

		for(Action a : actions)
			cp.add(new JButton(a));

		return cp;
	}
}
